package com.android.gilastv.ui.view;

import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.util.DateUtil;
import com.android.baselibrary.util.NetUtils;
import com.android.gilastv.R;
import com.android.gilastv.adapter.MenuListAdapter;
import com.android.gilastv.bean.MenuInfo;
import com.android.gilastv.manager.UserManager;
import com.android.gilastv.ui.activity.LoginActivity;
import com.android.gilastv.ui.activity.MyActivity;
import com.android.gilastv.ui.activity.SearchActivity;

import java.util.List;

/**
 * 标题控件
 */
public class Home_MenuView extends LinearLayout implements View.OnClickListener {
    public Home_MenuView(Context context) {
        super(context);
        this.context = context;
        initView(null);
    }

    public Home_MenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        initView(attrs);
    }

    public Home_MenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView(attrs);
    }

    Context context;
    RecyclerView recyclerview;
    LinearLayout ll_my,ll_search;
    TextView tv_time;
    ImageView  iv_net, iv_wifi;

    Handler handler = new Handler();

    MenuListAdapter adapter;
    List<MenuInfo> list;
    CommCallBack callBack;

    private void initView(AttributeSet attrs) {
        View.inflate(context, R.layout.view_home_menu, this);
        recyclerview = findViewById(R.id.recyclerview);
        ll_my = findViewById(R.id.ll_my);
        ll_my.setOnClickListener(this);
        ll_search = findViewById(R.id.ll_search);
        ll_search.setOnClickListener(this);
        tv_time = findViewById(R.id.tv_time);
        iv_net = findViewById(R.id.iv_net);
        iv_wifi = findViewById(R.id.iv_wifi);

        recyclerview.setLayoutManager(new LinearLayoutManager(context));
        adapter = new MenuListAdapter(context, callBack);
        recyclerview.setAdapter(adapter);

        handler.post(runnable);
//        setData(MenuInfo.getList());
    }

    public void setData(List<MenuInfo> list) {
        this.list = list;
        adapter.setData(this.list);
    }

    public void setSelect(int index) {
        adapter.setSelect(index);
    }

    public void setCallBack(CommCallBack callBack) {
        this.callBack = callBack;
        adapter.setCallBack(callBack);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_my:
                if (UserManager.isLogin(context)) {
                    context.startActivity(new Intent(context, MyActivity.class));
                } else {
                    context.startActivity(new Intent(context, LoginActivity.class));
                }
                break;
            case R.id.ll_search:  //搜索
                context.startActivity(new Intent(context, SearchActivity.class));
                break;
        }
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            tv_time.setText(DateUtil.getDateFromTimeLine(System.currentTimeMillis(), "HH:mm"));
//            pb_percent.setProgress(batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY));
            if (NetUtils.isWifiConnected(context)) {
                iv_wifi.setVisibility(VISIBLE);
            } else {
                iv_wifi.setVisibility(GONE);
            }
            if (TextUtils.isEmpty(NetUtils.getOperator(context))) {
                iv_net.setVisibility(GONE);
            } else {
                iv_net.setVisibility(VISIBLE);
            }

            handler.postDelayed(runnable, 1000 * 2);
        }
    };
}
